﻿/**
 * Class: OpenLayers.Layer.PoiLayer
 * POI图层，用于显示POI图层
 *
 * 继承自:
 *  - <OpenLayers.Layer>
 * @requires extend/Layer/PoiLayer.js
 */
OpenLayers.Layer.PageNationPoiLayer = OpenLayers.Class(OpenLayers.Layer.PoiLayer, {
	pagesize: 10,
	currentpage: 0,
	icons: null,
	filtereddata: null,
    /**
     * Constructor: OpenLayers.Layer.PoiLayer
     * Create a PoiLayer.
     *
     * Parameters:
     * name - {String}
     * options - {Object} Hashtable of extra options to tag onto the layer
     */
    initialize: function(name, options){
        OpenLayers.Layer.PoiLayer.prototype.initialize.apply(this, arguments);
    },
    
    /**
     *服务器端poi数据返回后构建marker数组，并设置marker鼠标事件
     *
     */
    createPois: function(bounds){
    	//如果filterByBounds为true,则在zoomend事件触发createPois函数清除上一视图的pois
    	if(this.filterByBounds){
    		this.clearPoiMarkers();
    	}
   		this.initCacheData();
   		if(this.dataSet){
   			var params = {
    			bounds: bounds.toBBOX(),
    			level: this.map.getZoom()
    		};
   			this.refreshPOIData(params);
   		}else{
   			//获取poi数据
   			this.data = this.getPoiData(bounds);
   			this.createMarkers();
   		}
    },
    
    refreshPOIData: function(params){
    	var self = this;
    	//请求数据
		this.dataSet.getPOIData(params, function(data){
			for(var type in data[0]){
				self.data = data[0][type];
				self.filtereddata = self.data;
				//创建marker
				self.createMarkers(function(){
					self.refreshMarkers();
					
					//创建marker
					if(self.createend){
						self.createend();
					}
				});
				
			}
		},function(){
			self.clearMarkers();
			alert("没有相关结果！");
		});
    },
    
    refreshMarkers: function(){
    	this.initCacheData();
    	
    	var total = this.filtereddata.length;
    	var start = this.currentpage * this.pagesize;
    	var end = start + this.pagesize;
    	end = Math.min(end, total);
    	
    	var index = 0;
		for(var i = start; i < end; i++){
			var poidata = this.filtereddata[i];
			var x = poidata.x || poidata.X;
        	var y = poidata.y || poidata.Y;
        	var lonlat = new OpenLayers.LonLat(x,y);
        	
        	this.markers[index].icon.display(true);
        	this.markers[index].lonlat = lonlat;
        	this.markers[index].data = poidata;
        	this.markers[index].typeName = poidata["type"] || poidata["TYPE"];
        	this.idmarkers[poidata.id || poidata.ID] = this.markers[index];
        	this.markers[index].id = poidata.id || poidata.ID;
        	this.drawMarker(this.markers[index]);
        	index ++;
		}
		for(var i = index; i < this.pagesize; i++){
			this.markers[i].icon.display(false);
			this.markers[i].id = null;
		}
    },
 	
    /**
     * 根据poi数据创建所有marker
     */
    createMarkers: function(cback) {
    	var scope = this;
    	this.createIcon(this.icons[0], function(poiicon){
    		for(var i = 0; i < this.pagesize; i++){
    			scope.createsingleMarker(i, poiicon);
    		}
    		cback ? cback() : false;
    	});
    },
    
    /**
    * 创建单个POI
    */
    createsingleMarker: function(index, defaulticon) {
    	var poiTypeicon = defaulticon.clone();
    	poiTypeicon.labelOffset = defaulticon.labelOffset;
    	poiTypeicon.setUrl(this.icons[index].icon);
		//为了提高绘制marker的效率将减少marker注册事件的BROWSER_EVENTS中的事件名称。
		//在创建完了所有的marker之后还原BROWSER_EVENTS。
		OpenLayers.Events.prototype.BROWSER_EVENTS = ["mouseout","mousemove","click"];
		
		var lonlat = this.map.getCenter();
		
		var marker = this.createMarker(poiTypeicon, lonlat);
		this.appendMarker(marker);
		
		//还原OpenLayers.Events.prototype.BROWSER_EVENTS
    	OpenLayers.Events.prototype.BROWSER_EVENTS = [
	        "mouseover", "mouseout",
	        "mousedown", "mouseup", "mousemove", 
	        "click", "dblclick", "rightclick", "dblrightclick",
	        "resize", "focus", "blur",
	        "touchstart", "touchmove", "touchend",
	        "keydown"
	    ];
   },
    
    
    /**
     * 添加marker对象
     */
    appendMarker: function(marker){
    	this.addMarker(marker);
    },
    
    /**
     * 创建marker对象
     */
    createMarker: function(icon, lonlat) {
    	var marker = new OpenLayers.Marker(lonlat,icon.clone());
    	marker.data = {};
       	var options = {
       		lonlat : lonlat,
       		owner	:	this,
       		marker	:	marker,
       		icon	: icon
       	};
       	//注册鼠标点击和鼠标滑过事件
       	marker.events.register('click', options, this.onMouseClick);
       	marker.events.register('mousemove', options, this.onMouseMove);
       	marker.events.register('mouseout', options, this.onMouseOut);
       	return marker;
    },
    
    /**
     *响应鼠标点击事件
     *
     */
    onMouseClick: function(evt){
    	var owner = this.owner;
	    //clickEnd外部自定义点击响应
	    if(owner.clickEnd != null && typeof owner.clickEnd == "function"){
	    	owner.clickEnd(this);
	    }
    },
    /**
     *响应鼠标滑过事件
     *
     */
    onMouseMove: function(evt){
    	//清除掉标注信息
    	var owner = this.owner;
    	owner.labelLayer.clear();
    	var poidata = this.marker.data;
    	
    	//添加标注
    	var title = poidata.title || poidata.TITLE;
    	
    	var label = new OpenLayers.Label(this.marker.lonlat, title, "poiLabel");
    	label.offset = this.icon.labelOffset;
    	owner.labelLayer.add(label);
    	
    	//高亮POI
    	if(!owner.isMarkerEqual(this.marker, owner.lastSelectedMarker)){
	    	owner.highlightPOI(this.marker);
	    }
    },
    
    onMouseOut: function() {
    	//清除掉标注信息
    	var owner = this.owner;
    	owner.labelLayer.clear();
		var cursor = this.owner.map.layerContainerDiv.style.cursor;
		this.owner.map.layerContainerDiv.style.cursor = "";
		this.owner.map.layerContainerDiv.style.cursor = cursor;
    },
    
    filterByTitle: function(title){
    	this.filter("TITLE",title);
    },
    
    filter: function(field, val){
    	var data = [];
    	for(var i in this.data){
    		if(this.data[i][field].indexOf(val) != -1){
    			data.push(this.data[i]);
    		}
    	}
    	
    	this.filtereddata = data;
    },
    
    CLASS_NAME: "OpenLayers.Layer.PageNationPoiLayer"
});



